# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=SDL2_mixer
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.6.3
pkgrel=1
pkgdesc="A simple multi-channel audio mixer (Version 2) (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/libsdl-org/SDL_mixer"
license=('spdx:Zlib')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-flac"
         "${MINGW_PACKAGE_PREFIX}-fluidsynth"
         "${MINGW_PACKAGE_PREFIX}-libvorbis"
         "${MINGW_PACKAGE_PREFIX}-libmodplug"
         "${MINGW_PACKAGE_PREFIX}-mpg123"
         "${MINGW_PACKAGE_PREFIX}-opusfile")
options=('staticlibs' 'strip')
source=("https://github.com/libsdl-org/SDL_mixer/releases/download/release-${pkgver}/SDL2_mixer-${pkgver}.zip"{,.sig})
sha256sums=('5488c377605c170b4db33bac0aac4a8f62e430cd6666bfe20ebca2b94b87923c'
            'SKIP')
validpgpkeys=('1528635D8053A57F77D1E08630A59377A7763BE6') # Sam Lantinga <slouken@libsdl.org>

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

}

build() {
  export lt_cv_deplibs_check_method='pass_all'

  local -a _common_config
  _common_config+=(
    --disable-music-ogg-shared
    --disable-music-flac-shared
    --enable-music-mp3
    --disable-music-mp3-mad-gpl
    --enable-music-mp3-mpg123
    --disable-music-mp3-mpg123-shared
    --enable-music-opus
    --disable-music-opus-shared
  )

  [[ -d "${srcdir}/build-${MSYSTEM}-static" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-static"
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  CPPFLAGS+=" -DFLAC__NO_DLL" \
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --disable-shared \
    --enable-static \
    "${_common_config[@]}"

  make

  [[ -d "${srcdir}/build-${MSYSTEM}-shared" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-shared"
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --disable-static \
    "${_common_config[@]}"

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  make DESTDIR="${pkgdir}" install

  cd "${srcdir}/build-${MSYSTEM}-shared"
  make DESTDIR="${pkgdir}" install

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/${_realname}/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"
}
